import requests
import random
import time
from datetime import datetime, timedelta

# 初始化时间戳为当前时间
timenow = datetime.now()
# timestamp = timenow.timestamp()

# 循环发送数据
while True:
    # 随机生成speed和flow
    up_speed = random.randint(55, 70)  # 随机速度55-70
    down_speed = random.randint(55, 70)  # 随机速度55-70
    up_flow = random.randint(100, 200)  # 随机车流量80-200
    down_flow = random.randint(100, 200)  # 随机车流量80-200

    # 将timestamp转换为ISO格式字符串
    # timestamp_str = timestamp.isoformat()

    # 构造POST请求的JSON数据
    data = {
        "up_speed": up_speed,
        "up_flow": up_flow,
        "down_speed": down_speed,
        "down_flow": down_flow,
        "timestamp": int(timenow.timestamp())
    }

    # 发送POST请求
    try:
        response = requests.post('http://127.0.0.1:5000/updatedata', json=data)
        print(f"数据发送成功！响应状态码：{response.status_code}")
        print(f"发送的数据：{data}")
    except requests.exceptions.RequestException as e:
        print(f"数据发送失败！错误信息：{e}")

    # 更新timestamp，增加5分钟
    timenow += timedelta(minutes=5)

    # 等待5秒钟
    time.sleep(5)